/******************************************************************************
 * * ui_nvm_interface.h - data structure of nvram
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef _UI_NVM_INTERFACE_H_
#define _UI_NVM_INTERFACE_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cpluscplus */

#ifndef __packed
#define __packed                       __attribute__((packed))
#endif
#include "../../lvgl/lvgl.h"

#define NV_MAX_APN_INFO_LEN (99+1)

typedef struct
{
    uint8_t apnflg;                                         // apn & iptype valid flag: 0-invalid, 1-valid
    uint8_t iptype;                                         // 0-IPV4V6; 1-IPV4; 2-IPV6
    char    apn[NV_MAX_APN_INFO_LEN];                       // APN
} __packed nv_apn_info;
typedef struct
{
    uint8_t ringvolume;                                     // refer to Hal interface
    uint8_t currentwallpaperindex;                          // current wall paper index
    uint8_t backlightintensity;                             // backlight intensity level
    uint8_t backlighttimeout;                               // backlight timeout value
    uint8_t currentlanguage;                                // current language setting
    uint8_t timesetflg;                                     // the time set flag 0:false,1:true
    uint8_t simid;                                          // sim id
    uint8_t sdautomount;                                    // sd card auto mount
    uint8_t voicewakeup;                                    // voice wakeup
    nv_apn_info apninfo[2];                                 // apn info
    uint8_t searchswitch;                                   // search switch
    uint8_t mobiledataswitch;                               // mobile data switch
    uint8_t flyingmodeswitch;                               // flying mode switch
} __packed nv_watch_settings_t;

#define NV_DIAL_MAX_ELEMENT_CNT   5
#define NV_DIAL_NAME_MAX_LEN      20
#define NV_WATCH_MAX_MENU_APP_CNT  33
#define NV_WATCH_MAX_LAUNCHER_TAB_CNT  6
typedef struct
{
    lv_point_t pos;
    uint8_t act_type;
}__packed nv_watch_dial_element_info;

typedef struct
{
    uint8_t dial_clock_type;                               // dial clock type
    uint8_t dial_psm_type;                                 // dial clock psm type
    char dial_name[NV_DIAL_NAME_MAX_LEN];
    nv_watch_dial_element_info element[NV_DIAL_MAX_ELEMENT_CNT];
    uint8_t menu_app_show[NV_WATCH_MAX_MENU_APP_CNT];
    uint8_t tab[NV_WATCH_MAX_LAUNCHER_TAB_CNT];
} __packed nv_watch_launcher_t;

#define NV_CONTACTS_MAX_NAME_LEN      (60+1)
#define NV_CONTACTS_MAX_NUMBER_LEN    (20+1)

typedef struct
{
    char    name[NV_CONTACTS_MAX_NAME_LEN];                 // ::string
    char    number[NV_CONTACTS_MAX_NUMBER_LEN];             // ::string
    uint8_t contact_type;                                   // 0:administrator, 1:standby administrator, 2: others
    uint8_t portrait_id;
} __packed nv_watch_phonebook_info_t;

#define NV_WATCH_MAX_CONTACT_NUM    30

typedef struct
{
    nv_watch_phonebook_info_t info[NV_WATCH_MAX_CONTACT_NUM];
} __packed nv_watch_phonebook_t;

#define NV_CALL_MAX_E_NUM_CNT     10
#define NV_CALL_MAX_E_NUM_SIZE    5
typedef struct
{
    uint8_t number_cnt;
    char    number[NV_CALL_MAX_E_NUM_CNT][NV_CALL_MAX_E_NUM_SIZE];   // ::string
} __packed nv_call_emergency_number_t;

typedef struct
{
    uint8_t                volume; // volume. See HAL_SPEAKER_GAIN in hal.h
    nv_call_emergency_number_t emergency_number;
} __packed nv_watch_phone_t;

/*pedometer*/
typedef struct
{
    uint8_t height;
    uint8_t weight;
    uint16_t goal;
} __packed nv_watch_pedometer_setting_t;

typedef struct
{
    nv_watch_pedometer_setting_t pedosetting;
} __packed nv_watch_pedometer_t;

#define NV_FACTORYMODE_TEST_NUM       16
#define NV_FACTORYMODE_AGINGTEST_NUM  4
typedef struct
{
    uint8_t test_res[NV_FACTORYMODE_TEST_NUM];
    uint8_t agingtest_res[NV_FACTORYMODE_AGINGTEST_NUM];
} __packed nv_watch_factorymode_t;

typedef struct
{
    uint8_t valid;                                                // 1: valid, 0: invalid
    uint8_t on_off;                                               // 1: alarm on, 0: alarm off
    uint8_t hour;                                                 // [0, 23]
    uint8_t min;                                                  // [0, 59]
    uint8_t repeat_bitmap;                                        /* Bit 7: 0 means alarm once, 1 means repeat alarm */
    /* Bit 0 ~ 6 are valid if bit 7 is 1 */
    /* Bit 0: Sunday, bit 1: Monday ... bit 6: Saturday */
} __packed nv_watch_alarm_info_t;

#define NV_ALARM_MAX_ALARM_NUM    5
typedef struct
{
    nv_watch_alarm_info_t alarm_info[NV_ALARM_MAX_ALARM_NUM];
} __packed nv_watch_alarm_t;

typedef struct
{
#define NV_WATCH_MAX_VOICE_MSG_GROUP_NUM  2
#define NV_WATCH_MAX_VOICE_MSG_CHAT_NUM   (NV_WATCH_MAX_CONTACT_NUM + NV_WATCH_MAX_VOICE_MSG_GROUP_NUM)
    uint8_t index[NV_WATCH_MAX_VOICE_MSG_CHAT_NUM]; /*index for nv_watch_phonebook_t,
                                                    *NV_WATCH_MAX_CONTACT_NUM is for family group,
                                                    *default 0xFF*/
    uint8_t mark[NV_WATCH_MAX_VOICE_MSG_CHAT_NUM];  /*unread messages for contacts,
                                                    *valid if index is valid
                                                    *default 0*/
} __packed nv_watch_voice_msg_t;

typedef struct
{
    uint8_t autoanswerflg;                                                // 1: auto answer enable, 0: auto answer disable
    uint8_t gsm_band_bitmap;                                       /* bit0: PGSM900, bit1: DCS1800, bit2: PCS1900, bit3: EGSM900,
                                                                    * bit4: GSM450, bit5: GSM480, bit6: GSM850
                                                                    */
    uint16_t lte_band_bitmap;                                      /* bit0: Band 1, bit1: Band 2, bit2: Band 3, bit3: Band 4,
                                                                    * bit4: Band 5, bit5: Band 7, bit6: Band 13, bit7: Band 17,
                                                                    * bit8: Band 20,bit9: Band 38, bit10: Band 39, bit11: Band 40,
                                                                    * bit12: Band 41
                                                                    */
} __packed nv_watch_engineer_mode_t;


#define NV_BLUETOOTH_MAX_DEV_CNT   10
#define NV_WATCH_BLUETOOTH_DEV_NAME_LEN  248
typedef struct
{
    uint8_t on_off;
    uint8_t dev_cnt;
    char dev_name[NV_BLUETOOTH_MAX_DEV_CNT][NV_WATCH_BLUETOOTH_DEV_NAME_LEN];
    char addr[NV_BLUETOOTH_MAX_DEV_CNT][6];
    uint32_t cod[NV_BLUETOOTH_MAX_DEV_CNT];
    uint8_t length[NV_BLUETOOTH_MAX_DEV_CNT];
} __packed nv_watch_bluetooth_t;

enum
{
    NV_AUDIO_ORDER_PLAY = 0,
    NV_AUDIO_SHUFFLE_PLAY,
    NV_AUDIO_ALL_REPEAT_PLAY,
    NV_AUDIO_REPEAT_ONCE_PLAY,
    NV_AUDIO_MODE_MAX
};

typedef uint8_t NV_AUDIO_PLAYER_MODE;

typedef struct
{
    NV_AUDIO_PLAYER_MODE audio_player_mode;
} __packed nv_watch_audio_player_t;

#define NV_ICCID_LEN   20
#define NV_IMSI_LEN    15

typedef struct
{
    char iccid1[NV_ICCID_LEN+1];
    char iccid2[NV_ICCID_LEN+1];
    char imsi1[NV_IMSI_LEN+1];
    char imsi2[NV_IMSI_LEN+1];
} __packed nv_watch_dm_t;

typedef struct
{
    char    number[NV_CONTACTS_MAX_NUMBER_LEN];             // ::string
    uint8_t call_status;                                    // 0:outgoing call, 1:incoming call, 2: missed call, 3: rejected call
    uint8_t month;                                          // [0, 12]
    uint8_t day;                                            // [0, 31]
    uint8_t hour;                                           // [0, 23]
    uint8_t min;                                            // [0, 59]
} __packed nv_watch_call_log_info_t;

#define NV_WATCH_MAX_CALL_LOG_NUM   30

typedef struct
{
    nv_watch_call_log_info_t info[NV_WATCH_MAX_CALL_LOG_NUM];
} __packed nv_watch_call_log_t;

typedef struct
{
    nv_watch_settings_t                    settings;
    nv_watch_phone_t                       phone;
    nv_watch_pedometer_t                   pedometer;
    nv_watch_launcher_t                    launcher;
    nv_watch_phonebook_t                   phonebook;
    nv_watch_factorymode_t                 factorymode;
    nv_watch_alarm_t                       alarm;
    nv_watch_voice_msg_t                   voicemsg;
    nv_watch_engineer_mode_t               engineermode;
    nv_watch_bluetooth_t                   bluetooth;
    nv_watch_audio_player_t                audioplayer;
    nv_watch_dm_t                          dm;
    nv_watch_call_log_t                    calllog;
} __packed nv_watch_stru_t;

enum
{
    NV_SECTION_UI_SETTINGS = 0,                         // nv_watch_settings_t
    NV_SECTION_UI_PHONE,                                // nv_watch_phone_t
    NV_SECTION_UI_PEDOMETER,                            // nv_watch_pedometer_t
    NV_SECTION_UI_LAUNCHER,                             // nv_watch_launcher_t
    NV_SECTION_UI_PHONEBOOK,                            // nv_watch_phonebook_t
    NV_SECTION_UI_FACTORYMODE,                          // nv_watch_factorymode_t
    NV_SECTION_UI_ALARM,                                // nv_watch_alarm_t
    NV_SECTION_UI_VOICE_MSG,                            // nv_watch_voice_msg_t
    NV_SECTION_UI_ENGINEER_MODE,                        // nv_watch_engineer_mode_t
    NV_SECTION_UI_BLUETOOTH,                            // nv_watch_bluetooth_t
    NV_SECTION_UI_AUDIO_PLAYER,                         // nv_watch_audio_player_t
    NV_SECTION_UI_DM,                                   // nv_watch_dm_t
    NV_SECTION_UI_CALL_LOG,                             // nv_watch_call_log_t
    NV_SECTION_UI_MAX_NUM
};
typedef uint8_t NV_UI_SECTION_ID;

/**
 * write to NVRAM
 * param (in) addr:  uint32_t*
 * param (in) data:  uint32_t*
 * param (in) len:   uint32_t
 * return  uint32_t: bytes have been writen
 */
extern uint32_t UI_NV_Write_Req(NV_UI_SECTION_ID SectionId, uint32_t OffSet, uint32_t Len, uint8_t *data);

/**
 * read data from NVRAM
 * param (in) SectionId:   UI_NV_SECTION_ID  section ID of NVM
 * param (in) OffSet:  uint32_t                Offset in the Section
 * param (in) len:   uint32_t                  Length of data to read
 * param (out) data:  uint32_t*                data buffer
 * return  uint32_t: bytes have been read
 */
extern uint32_t UI_NV_Read_Req(NV_UI_SECTION_ID SectionId, uint32_t OffSet, uint32_t Len, uint8_t *data);

/**
 * write default value to NVRAM for factory restore
 * return  void:
 */
extern void UI_NV_Restroe_Req(void);

#ifdef __cplusplus
}
#endif /* __cpluscplus */

#endif /* end of _UI_NVM_STRU_H_ */
